package cn.hg.solon.youcan.system.provider;

import static cn.hg.solon.youcan.system.entity.table.SysUserOnlineTableDef.SYS_USER_ONLINE;

import java.util.Date;
import java.util.Map;

import org.dromara.hutool.core.text.CharSequenceUtil;
import org.dromara.hutool.core.text.StrValidator;
import org.dromara.hutool.core.util.ObjUtil;
import org.noear.solon.annotation.Component;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryOrderBy;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import com.mybatisflex.solon.service.impl.ServiceImpl;

import cn.hg.solon.youcan.common.enums.OnlineStatus;
import cn.hg.solon.youcan.system.entity.SysUserOnline;
import cn.hg.solon.youcan.system.mapper.SysUserOnlineMapper;
import cn.hg.solon.youcan.system.service.SysUserOnlineService;

/**
 * @author 胡高
 */
@Component
public class SysUserOnlineProvider extends ServiceImpl<SysUserOnlineMapper, SysUserOnline>
implements SysUserOnlineService {

    @Override
    public boolean offline(String token) {
        SysUserOnline entity = UpdateEntity.of(SysUserOnline.class);
        entity.setId(token);
        entity.setStatus(OnlineStatus.OFF.name());
        return this.updateById(entity);
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.SysUserOnlineService#pageByMap(com.mybatisflex.core.paginate.Page, java.util.Map)
     */
    @Override
    public Page<SysUserOnline> pageByMap(Page<SysUserOnline> page, Map<String, Object> map) {
        String word = (String)map.get("word");
        String status = (String)map.get("status");
        Date startDate = (Date)map.get("startDate");
        Date endDate = (Date)map.get("endDate");
        String sortField = (String)map.get("sortField");
        String sortType = (String)map.get("sortType");

        QueryWrapper query = QueryWrapper.create()
            .where(SYS_USER_ONLINE.STATUS.eq(status).when(StrValidator.isNotBlank(status))
                .and(SYS_USER_ONLINE.START_DATETIME.ge(startDate).when(ObjUtil.isNotNull(startDate)))
                .and(SYS_USER_ONLINE.START_DATETIME.le(endDate).when(ObjUtil.isNotNull(endDate)))
                .and(SYS_USER_ONLINE.BROWSER.like(word).when(StrValidator.isNotBlank(word))
                    .or(SYS_USER_ONLINE.DEPT_NAME.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_USER_ONLINE.LOCATION.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_USER_ONLINE.NICKNAME.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_USER_ONLINE.OS.like(word).when(StrValidator.isNotBlank(word)))
                )
            );

        if (StrValidator.isNotBlank(sortField) && StrValidator.isNotBlank(sortType)) {
            query.orderBy(new QueryOrderBy(new QueryColumn(CharSequenceUtil.toUnderlineCase(sortField)), sortType));
        }

        return this.page(page, query);

    }

    @Override
    public void syncActivityTime(String token, Date datetime) {
        SysUserOnline entity = UpdateEntity.of(SysUserOnline.class, token);
        entity.setActivityDatetime(datetime);

        this.updateById(entity);
    }

}
